/***************************************************************************
创建者：　华磊
 开始时间：               : 2020.11.10
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************
 *   程序文件视窗测试用代理类
*                                                                         *
 ***************************************************************************/
#include "programfiledelegate.h"
#include <QDebug>
#include <QDirModel>
#include <QFileInfo>
#include <QDateTime>
#include <QSettings>
#include "../CommunicateDelegate/communicatenormaldelegate.h"
#include <QMessageBox>
#include "hmicommondefine.h"
#include "instructionEnum.h"


extern FileData fileData;
extern FtpManage *ftpManage;

ProgramFileDelegate::ProgramFileDelegate(ProgramFileView *programFileViewIn,
                GlobalData *globalDataIn, CommunicateNormalDelegate *communicateNormalDelegateIn,
                                         QObject *parent):QObject(parent)
{
    globalData = globalDataIn;
    programFileView=programFileViewIn;
    communicateNormalDelegate=communicateNormalDelegateIn;
    initConnect();
    initializeView();

}

void ProgramFileDelegate::initConnect()
{
    connect(programFileView->newfileView, SIGNAL(confirmNewFile()), this, SLOT(newFileSlot()));
    connect(programFileView->settingProperty,SIGNAL(saveSettingProperty()),this,SLOT(saveFileSlot()));
    connect(programFileView, SIGNAL(openFile(QString)), this, SLOT(openFileSlot(QString)));
    connect(programFileView->deleteDialog, SIGNAL(deleteFile(QString)), this, SLOT(deleteFileSlot(QString)));
    connect(programFileView->copySettingDialog, SIGNAL(copyFile(QString,QString)),this,SLOT(copyFileSlot(QString,QString)));
    connect(programFileView, SIGNAL(defaultSetting(QString)), this, SLOT(defaultSettingSlot(QString)));
    connect(programFileView, SIGNAL(settingFileProperty(ProgramFileView::FileProperty )), this, SLOT(settingFilePropertySlot(ProgramFileView::FileProperty )));
    connect(programFileView, SIGNAL(selectedFileChanged(QString, const QModelIndex)), this, SLOT(selectedFileChangedSlot(QString, const QModelIndex)));
    // 当新建文件上传完成后,通知控制器打开新建文件
    connect(ftpManage, SIGNAL(isPutFile(bool,QString)), this, SLOT(openNewFileCommunicationSlot(bool,QString)));

    // programView界面手动刷新程序文件列表
    connect(programFileView, SIGNAL(updateProgramFileList_Signal()), this, SLOT(updateMountFileSlot()));
}

ProgramFileDelegate::~ProgramFileDelegate()
{

}

void ProgramFileDelegate::  newFileSlot()
{
    tmpFileWholeName = ".txt";
    tmpFileWholeName = programFileView->newfileView->newFileName + tmpFileWholeName;
//    if(programFileView->newfileView->newFileName.isEmpty())
//    {
//        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("消息"),tr("请输入文件名!"));
//        return;
//    }
    if(programFileView->remoteProgramFileList.contains(tmpFileWholeName))
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("消息"),tr("该文件名已存在,请修改文件名"));
        return;
    }

    //转换utf-8
    QByteArray tmp;
    tmp = tmpFileWholeName.toLocal8Bit();
    tmpFileWholeName = QString(tmp.data());
    qDebug() << "new *****"<<tmpFileWholeName;

    //检查
    if(tmpFileWholeName.split(" ").size()>=2)
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("文件名不允许存在空格!"));
        return;//
    }
    if(tmpFileWholeName == "" || tmpFileWholeName == ".txt")
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("文件名不可为空!"));
        return;//名字为空
    }
//    QFileInfo fileCheck(fileData.currentRobotFileAddress+"program/" + tmpFileWholeName);
//    if(fileCheck.isFile())
//    {
//        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("消息"),tr("该文件名已存在,请修改文件名"));
//        return;
//    }

    if(globalData)
    {
//        if(globalData->tryLock(TRYLOCKTIME))
//        {
            if(globalData->getUserInfo().account < EM_ACCOUNT_ADMIN)
            {
//                globalData->unlock();
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("您当前用户没有操作权限\n请切换用户角色"));
                return ;
            }

            if(globalData->globalCurrentState == ENUM_COMMAND_RUN || globalData->globalCurrentState == ENUM_COMMAND_CEASE )
            {
                //qDebug()<<"globalData->globalCurrentState = "<<globalData->globalCurrentState;
//                globalData->unlock();
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("机器程序正在运行中\n请点击停止按钮后再新建程序"));
                qDebug()<<tr("机器程序正在运行中\n请点击停止按钮后再新建程序");
                return ;
            }

            if(globalData->globalLoginStatus)
            {
                //清空所有缓存
                fileData.cmdStrWriteList.clear();
                fileData.getPointProList.clear();
                fileData.palletStackInfo.clear();
                fileData.regPlList.clear();
                // 清除DP点位数据
                qDebug()<<"fileData.vehicleDPointList.clear11111111111111111111111111";
                fileData.vehicleDPointList.clear();

                //写一个空文件

                QString nowDateTime = QString::fromStdString(globalData->getCurrentTime());
                //globalData->unlock();

                fileData.fileProInfo.fileName = tmpFileWholeName.toStdString();
                fileData.fileProInfo.version = programFileView->newfileView->newFileVersion.toStdString();
                fileData.fileProInfo.owner = programFileView->newfileView->newFileOwner.toStdString();
                fileData.fileProInfo.comment = programFileView->newfileView->newFileNote.toStdString();

                fileData.fileProInfo.programName = programFileView->newfileView->newFileName.toStdString();//
                fileData.fileProInfo.programSize = 0;

                QStringList list = nowDateTime.split(" ");
                QString nowCreatTime = "";
                if(list.size() == 2)
                {
                    nowCreatTime =  list[0] + " " + list[1];
                }
                fileData.fileProInfo.createTime = nowCreatTime.toStdString();
                fileData.fileProInfo.modifiTime = nowCreatTime.toStdString();
                fileData.fileProInfo.lineCount = 0;
                fileData.fileProInfo.memorySize = 0;
                fileData.fileProInfo.protectPro = "";//?

                fileData.fileProInfo.templateType = 0;//中英文模式

                fileData.fileProInfo.tcdStackSize = 0;
                fileData.fileProInfo.tcdTaskPriority = 0;
                fileData.fileProInfo.tcdTimeSlice = 0;
                fileData.fileProInfo.tcdBusyLampOff = 0;
                fileData.fileProInfo.tcdAbortRequest = 0;
                fileData.fileProInfo.tcdPauseRequest = 0;

                fileData.fileProInfo.defaultGroup = "";
                fileData.fileProInfo.controlCode = "";

                //预先载入空点
                fileData.globalDefaultUserNum = globalData->globalDefaultUserNum;
                fileData.globalDefaultToolNum = globalData->globalDefaultToolNum;

                PointPro tmpPoint;//目前默认载入关节点 减少文件大小(约24k)

                globalData->getCurrentArmPosition_joint(tmpPoint);


//                globalData->unlock();

                for(int i = 0;i<5;i++)//已经动态;不需要那么多个点
                {
                    fileData.getPointProList.append(tmpPoint);
                    tmpPoint.pointNum++;
                }

                // 写入新建文件
                fileData.writeProgramFile(tmpFileWholeName, true);
//                this->saveFileSlot();
                // 使用GSOAP传输文件完成
                if(!GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
                {
                    if(globalData->isUseFtpOrNfs)
                    {
                        // 增加打开新建文件标识,当FTP将新建文件上传完成后,通知控制器打开新建文件
                        fileData.ftpOperateNum = EM_FTP_OPERATE_CREATE_FILE;
                        ftpManage->ftpPutFile(tmpFileWholeName,(fileData.currentRobotFileAddress + "program/"),PROGRAM_PATH);
                    }
                }
                else
                {
                    // 更新远程文件列表
                    this->updateMountFileSlot();
                }

                //再 读文件(打开文件)
                openFileSlot(tmpFileWholeName);//新建的文件名,打开(名字带txt)

                //读取有问题 补回界面上缓存的 点数据
                // 有内存越界风险
                tmpPoint.pointNum = 1;//点位1开始
                for(int i = 0;i<fileData.getPointProList.size();i++)//恢复一位问题
                {
                    fileData.getPointProList.replace(i,tmpPoint);
                    tmpPoint.pointNum++;
                }
            }
//            else
//            {
//                globalData->unlock();
//            }
//        }
    }
    //emit changeViewIDsignal(MID_VIEW_PROGRAM);//无需 openfile本身就已经有这个操作
}


void ProgramFileDelegate::openFileSlot(QString fileName)
{
    //qDebug() << "open: " << fileName;

    if(globalData)
    {
//        if(globalData->tryLock(TRYLOCKTIME))
//        {
            if(globalData->getUserInfo().account < EM_ACCOUNT_ADMIN)
            {
//                globalData->unlock();
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("您当前用户没有操作权限\n请切换用户角色"));
                return ;
            }

            if(globalData->globalCurrentState == ENUM_COMMAND_RUN || globalData->globalCurrentState == ENUM_COMMAND_CEASE )
            {
                qDebug()<<"globalData->globalCurrentState = "<<globalData->globalCurrentState;
//                globalData->unlock();
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("机器程序正在运行中\n请点击停止按钮后再打开程序"));
                qDebug()<<tr("机器程序正在运行中\n请点击停止按钮后再打开程序");
                return ;
            }

//            globalData->unlock();
//        }

        //2017.05.11 参考旧版hmi:补充一个判断,如果打开的是同一个文件,不操作??
//        bool isSameFile = false;
        if(fileData.currentFileName == fileName)
        {
            emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("程序文件已处于打开状态！\n"));
        }


//        if(!isSameFile)//不是同一个文件 重新打开
//        {
            //清空点数据缓存
            fileData.getPointProList.clear();
            fileData.palletStackInfo.clear();
            fileData.regPlList.clear();
            fileData.vehicleDPointList.clear();

            // 读取文件
            fileData.readProgram(fileName);

            // 读取点位绘制到地图上
            emit showPointInMap();
//        }
    }

    //qDebug()<<"file name:"<<globalData->currentFile;
    //qDebug()<<"program name:"<<globalData->currentProgramName;
    //qDebug()<<"list:"<<globalData->cmdStrList;
    if(0==fileData.fileProInfo.programName.size())
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("2002:此文件名为空,打开失败!"));
    }
    else
    {
        emit openFileCommSignal(fileName);
        emit openFileEditSignal();
        fileData.cmdStrWriteSuccess = true;//存在一种情况:runview已有缓存;但再次重新打开文件:不刷新

        //fileData.fileOpenFirst = true;
        emit changeViewIDsignal(MID_VIEW_PROGRAM);
    }
}


void ProgramFileDelegate::deleteFileSlot(QString fileName)
{
    if(fileName == fileData.currentFileName)
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("该文件正在编辑中\n不可删除"));
        return ;
    }

//    if(globalData->tryLock(TRYLOCKTIME))
//    {
        if(globalData->globalCurrentState == ENUM_COMMAND_RUN || globalData->globalCurrentState == ENUM_COMMAND_CEASE )
        {
            qDebug()<<"globalData->globalCurrentState = "<<globalData->globalCurrentState;
//            globalData->unlock();
            emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("机器程序正在运行中\n请点击停止按钮后再删除程序"));
            qDebug()<<tr("机器程序正在运行中\n请点击停止按钮后再删除程序");
            return ;
        }

//        globalData->unlock();
//    }

    //qDebug() << "delete: " << fileName;
    //QString tmp = "rm "+QString(PROGRAM_PATH)+fileName;
    if(fileData.currentRobotFileAddress != "")
    {
        // 删除本地文件
        QString tmp = fileData.currentRobotFileAddress + "program/"+fileName;
//        system(tmp.toLocal8Bit().data());//中文支持
        
        if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
        {
            int returnFlag;
            communicateNormalDelegate->removeRobotProgramFile(globalData->currentRobotID, fileName.toStdString(), returnFlag);
            if(returnFlag == 0)
            {
                qDebug() << " ProgramFileDelegate::deleteFileSlot :" << tmp << "Gsoap删除文件成功!";
                // 更新文件列表
                this->updateMountFileSlot();
            }
            else
            {
                qDebug() << " ProgramFileDelegate::deleteFileSlot :" << tmp << "Gsoap删除文件失败!";
            }
        }
        else
        {
            // 删除本地文件
            QFile removeFile(tmp);
            if(removeFile.exists())
            {
                // 如果存在就删除
                // 删除之前要关闭文件，否则造成无法删除
                removeFile.close();
                if(removeFile.remove())
                {
                    qDebug() << "ProgramFileDelegate::deleteFileSlot  == 本地文件:" << tmp << " 删除成功!";
                }
                else
                {
                   qDebug() << "ProgramFileDelegate::deleteFileSlot  == 本地文件:" << tmp << " 删除失败," << removeFile.errorString();
                }
            }

            if(globalData->isUseFtpOrNfs)
            {
                // 删除FTP服务器中program目录下的程序文件
                ftpManage->ftpRemoveFile(fileName, PROGRAM_PATH);
            }
        }

        // 文件属性初始化
        programFileView->fileProperty.fileName = "";        //文件名
        programFileView->fileProperty.version = "";         //文件版本号
        programFileView->fileProperty.owner = "";           //文件所有者
        programFileView->fileProperty.createTime = "";      //文件创建时间
        programFileView->fileProperty.modifyTime = "";      //文件最后一次的修改时间
        programFileView->fileProperty.note = "";            //文件注释
        programFileView->fileProperty.type = "";            //文件类型

        //fileData.currentFileName = "";

        programFileView->updateView();
    }
}


void ProgramFileDelegate::copyFileSlot(QString beforeName, QString newName)
{
    if(newName.split(" ").size()>=2)
    {
        programFileView->copySettingDialog->msgReturn(tr("文件名存在空格"));
        return;//
    }
    if(newName == beforeName)
    {
        programFileView->copySettingDialog->msgReturn(tr("文件名重复,请修改其他名字"));
        return;//重名
    }
    if(newName == "" || newName == ".txt")
    {
        programFileView->copySettingDialog->msgReturn(tr("文件名不可为空"));
        return;//名字为空
    }
//    QFile tmpFile(fileData.currentRobotFileAddress + "program/"+newName);
//    if(tmpFile.exists())
//    {
//        programFileView->copySettingDialog->msgReturn(tr("文件名重复,请修改其他名字"));
//        return;//已存在
//    }
    if(programFileView->remoteProgramFileList.contains(newName))
    {
        programFileView->copySettingDialog->msgReturn(tr("文件名重复,请修改其他名字"));
        return;//已存在
    }

    QString cpFileName = "";
    if(fileData.currentRobotFileAddress!="")
    {
        QString orFileName = fileData.currentRobotFileAddress + "program/"+beforeName;
        cpFileName = fileData.currentRobotFileAddress + "program/"+newName;
//        QString tmp = "cp "+ orFileName +" "+ cpFileName;
//        //qDebug() << "copy: " << tmp;
//        system(tmp.toLocal8Bit().data());//中文支持
        QFile::copy(orFileName, cpFileName);
        programFileView->copySettingDialog->msgReturn("");//清空提示
        programFileView->copySettingDialog->hide();//隐藏

        programFileView->updateView();

        if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
        {
            // GSOAP复制文件
            // 先gsoap读取要复制的文件，再调用GSOAP接口传输给控制器，完成创建复制
            std::vector <std::string> fileStrList;
            if(0)

            {
                //            QFile newFile(cpFileName);
                //            if(newFile.open(QIODevice::ReadOnly | QIODevice::Text))
                //            {
                //                newFile.reset();

                //                QTextStream in(&newFile);

                //                while (!in.atEnd()) {

                //                    // 逐行读取并保存于容器中
                //                    fileStrList.push_back(in.readLine().toStdString());
                //                }
                //                newFile.close();
                //                newFile.flush();
                //            }

            }
            int returnFlag;
            // 读取远程文件
            communicateNormalDelegate->readRobotProgramFile(globalData->currentRobotID,beforeName.toStdString(), fileStrList, returnFlag);
            if(returnFlag == 0)
            {
                // 写文件
                communicateNormalDelegate->writeRobotProgramFile(globalData->currentRobotID, newName.toStdString(), fileStrList, returnFlag);
                if(returnFlag == 0)
                {
                    qDebug() << "ProgramFileDelegate::copyFileSlot gsoap复制文件成功:" << cpFileName;
                    // 更新文件列表
                    this->updateMountFileSlot();
                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("复制文件成功!"));
                }
                else
                {
                    qDebug() << "ProgramFileDelegate::copyFileSlot gsoap复制文件失败:" << cpFileName;
                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("复制文件失败!"));
                    return;
                }
            }
        }
        else
        {
            if(globalData->isUseFtpOrNfs)
            {
                // 复制一个程序文件到 FTP服务器program目录下
                ftpManage->ftpPutFile(newName, (fileData.currentRobotFileAddress + "program/"), PROGRAM_PATH);
            }
        }
    }
    else
    {
        programFileView->copySettingDialog->msgReturn(tr("文件路径数据异常,请重启"));
    }
//    QTime time = QTime::currentTime().addMSecs(2000);
//    while(QTime::currentTime() < time)
//    {
//        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);
//    }

}


void ProgramFileDelegate::defaultSettingSlot(QString fileName)
{
    qDebug() << "defualuSetting: " << fileName;

//    if(globalData->tryLock(TRYLOCKTIME))
//    {
        if(globalData->globalCurrentState == ENUM_COMMAND_RUN || globalData->globalCurrentState == ENUM_COMMAND_CEASE )
        {
            //qDebug()<<"globalData->globalCurrentState = "<<globalData->globalCurrentState;
//            globalData->unlock();
            emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("机器程序正在运行中\n请点击停止按钮后再设置缺省程序"));
            qDebug()<<tr("机器程序正在运行中\n请点击停止按钮后再设置缺省程序");
            return ;
        }

//        globalData->unlock();
//    }

    QSettings configIniWrite(fileData.currentRobotFileAddress+"/config/defaultProgram.ini", QSettings::IniFormat);
    configIniWrite.setValue("/defaultProgram/name", fileName);

    int returnFlag;
    communicateNormalDelegate->setDefaultProgramFile(fileName.toStdString(), returnFlag);
    if(returnFlag == 0)
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("缺省文件修改完成"));
    }
    else
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("缺省文件修改失败"));
    }

}


void ProgramFileDelegate::settingFilePropertySlot(ProgramFileView::FileProperty fileProperty)
{
    //qDebug() << "settingProperty: " << fileProperty.fileName;

    if(fileProperty.fileName == "")
    {
        return ;
    }
    //读文件
    if(globalData)
    {
//        if(globalData->tryLock(TRYLOCKTIME))
//        {
            if(globalData->getUserInfo().account < EM_ACCOUNT_ADMIN)
            {
//                globalData->unlock();
                emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("您当前用户没有操作权限\n请切换用户角色"));
                return ;
            }

//            globalData->unlock();
//        }

        //if(globalData->globalLoginStatus)
        {
            //清空点数据缓存
            fileData.getPointProList.clear();
            fileData.palletStackInfo.clear();
            fileData.regPlList.clear();

            fileData.readProgram(fileProperty.fileName);//这边最好处理一个判断(读文件是否成功)
        }
        //else
        {
        }
    }
    //更新界面内容
    programFileView->settingProperty->tmpName = fileProperty.fileName.left(fileProperty.fileName.size()-4);
    programFileView->settingProperty->tmpVer = QString::fromStdString(fileData.fileProInfo.version);
    programFileView->settingProperty->tmpOwner = QString::fromStdString(fileData.fileProInfo.owner);
    programFileView->settingProperty->tmpNote = tr(fileData.fileProInfo.comment.data());
    programFileView->settingProperty->showFileInfo();

    //emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("文件打开成功!"));

    if(0==fileProperty.fileName.size())
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("2001:此文件名为空,打开失败!"));
        return;
    }

    emit openFileCommSignal(fileProperty.fileName);
    emit openFileEditSignal();

    //emit changeViewIDsignal(MID_VIEW_PROGRAM);//?不知道为何添加
}

void ProgramFileDelegate::initializeView()
{
    programFileView->fileString << "firstFile";

    //programFileView->dirModel = new QFileSystemModel;
    //programFileView->index = programFileView->dirModel->index(PROGRAM_PATH);

    programFileView->fileProperty.fileName = "";
    programFileView->fileProperty.version = "";
    programFileView->fileProperty.owner = "";
    programFileView->fileProperty.createTime = "";
    programFileView->fileProperty.modifyTime = "";
    programFileView->fileProperty.note = "";
    programFileView->fileProperty.type = "";

    programFileView->updateView();
}

void ProgramFileDelegate::updateMountFileSlot()
{
    if(GSOAP_OPERATE_PROGRAM_FILE_SWITCH)
    {
        int returnFlag;
        std::vector <std::string> fileList;
        communicateNormalDelegate->getRobotProgramFileList(globalData->currentRobotID, fileList, returnFlag);
        if(returnFlag == 0)
        {
            // 获取文件成功
            programFileView->remoteProgramFileList.clear();
            for(int i = 0; i < (int)fileList.size(); i++)
            {
                programFileView->remoteProgramFileList << (QString::fromStdString(fileList.at(i)));
            }
        }
        // 通信失败
        else
        {
             QMessageBox::information(NULL, "Information", "Gsoap获取远程程序文件列表失败！", QMessageBox::Yes );
        }
    }

    // 更新显示
    programFileView->mountFileUpdate();
}

void ProgramFileDelegate::selectedFileChangedSlot(QString fileName, const QModelIndex &index)
{
    //qDebug()<< "row:"<<index.row();
    programFileView->fileProperty.fileName = fileName;

    //qDebug()<< "fileName:"<<fileName;

    if(globalData)
    {
        // 快速读取程序文件，获取文件属性
        if(fileData.readProgramQuick(fileName) == 0)
        {
            programFileView->fileProperty.owner = QString::fromStdString(fileData.fileProInfoQuick.owner);
            programFileView->fileProperty.version = QString::fromStdString(fileData.fileProInfoQuick.version);
            programFileView->fileProperty.createTime = QString::fromStdString(fileData.fileProInfoQuick.createTime);
            programFileView->fileProperty.note = tr(fileData.fileProInfoQuick.comment.data());
        }
    }

    programFileView->updateView();
}

void ProgramFileDelegate::saveFileSlot()
{
    QString nowDateTime = "";
//    if(globalData->tryLock(TRYLOCKTIME))
//    {
        nowDateTime = QString::fromStdString(globalData->getCurrentTime());
//        globalData->unlock();
//    }

    programFileView->settingProperty->getFileInfo();
    QString tmp= programFileView->settingProperty->tmpName + ".txt";

    fileData.fileProInfo.fileName = tmp.toStdString();
    fileData.fileProInfo.version = programFileView->settingProperty->tmpVer.toStdString();
    fileData.fileProInfo.owner = programFileView->settingProperty->tmpOwner.toStdString();
    fileData.fileProInfo.comment = programFileView->settingProperty->tmpNote.toStdString();
    QStringList list = nowDateTime.split(" ");
    QString nowCreatTime = "";
    if(list.size() == 2)
    {
        nowCreatTime = "DATE " + list[0] + " TIME " + list[1];
    }
    fileData.fileProInfo.createTime = nowCreatTime.toStdString();
    fileData.fileProInfo.modifiTime = nowCreatTime.toStdString();

    fileData.writeProgramFile(fileData.currentFileName,true);

    if(0==tmp.size())
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("2003:此文件名为空,打开失败!"));
        return;
    }
    emit openFileCommSignal(tmp);
    emit openFileEditSignal();
    //fileData.fileOpenFirst = true;//补充 重复赋值true  因为下面的切换页面需要?

    emit changeViewIDsignal(MID_VIEW_PROGRAM);
}

void ProgramFileDelegate::changeLanguageSlot(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/proFileDelEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    programFileView->changeLanguage(language);
}

void ProgramFileDelegate::openNewFileCommunicationSlot(bool result, const QString &message)
{
    if(fileData.ftpOperateNum != EM_FTP_OPERATE_CREATE_FILE)
    {
        return;
    }
    if(result)
    {
        //再 读文件(打开文件)
        openFileSlot(tmpFileWholeName);//新建的文件名,打开(名字带txt)
        openNewFileFlag = false;
    }

    fileData.ftpOperateNum = EM_FTP_OPERATE_CLOSE;
}
